package com.css.zfzx.fwsjcj.modules.structure.controller;

import com.alibaba.fastjson.JSONObject;
import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.base.response.RestResponse;
import com.css.bpm.platform.org.role.repository.entity.RoleEntity;
import com.css.bpm.platform.org.user.repository.entity.UserEntity;
import com.css.bpm.platform.utils.PlatformPageUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.fwsjcj.cache.DivisionCache;
import com.css.zfzx.fwsjcj.commen.util.Constant;
import com.css.zfzx.fwsjcj.commen.util.DownloadUtils;
import com.css.zfzx.fwsjcj.modules.analysis.vo.AnalysisQueryVo;
import com.css.zfzx.fwsjcj.modules.reinforce.vo.QueryVO;
import com.css.zfzx.fwsjcj.modules.structure.entity.FwStructureFormEntity;
import com.css.zfzx.fwsjcj.modules.structure.service.FwStructureFormService;
import com.css.zfzx.fwsjcj.modules.structure.vo.CheckVo;
import com.css.zfzx.fwsjcj.modules.structure.vo.StructureQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author leon
 * @Title:
 * @Description: 房屋结构主表Constroller
 * @date 2020/6/2 15:38
 */
@RestController
@RequestMapping("/fw/structure")
@Slf4j
public class FwStructureFormController {
    @Autowired
    private FwStructureFormService structureFormService;
    @Autowired
    DownloadUtils downloadUtils;

    /**
     * 增加
     *
     * @param fwStructureFormEntity
     * @return
     */
    @PostMapping("save")
    public RestResponse save(@RequestBody FwStructureFormEntity fwStructureFormEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.save(fwStructureFormEntity);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 修改
     *
     * @param fwStructureFormEntity
     * @return
     */
    @PostMapping("update")
    public RestResponse update(@RequestBody FwStructureFormEntity fwStructureFormEntity) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.update(fwStructureFormEntity);
            jsonObject.put("message", "修改成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "修改失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 不区分审核状态的分页
     *
     * @param request
     * @param structureQueryVo
     * @return
     */
    @GetMapping("page")
    public RestResponse getPage(HttpServletRequest request, StructureQueryVo structureQueryVo) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = structureFormService.getPage(structureQueryVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 审核模块的分页   详查模块
     *
     * @param request
     * @param structureQueryVo
     * @return
     */
    @GetMapping("check/page")
    public RestResponse getCheckPage(HttpServletRequest request, StructureQueryVo structureQueryVo) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = structureFormService.getCheckPage(structureQueryVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 根据ID获取信息
     *
     * @param id
     * @return
     */
    @GetMapping("getone/{id}")
    public RestResponse getOne(@PathVariable String id) {
        RestResponse restResponse = null;
        try {
            FwStructureFormEntity structureFormEntity = structureFormService.findById(id);
            restResponse = RestResponse.succeed(structureFormEntity);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 根据ID获取子表信息
     *
     * @param id
     * @return
     */
    @GetMapping("getChild/{id}")
    public RestResponse getChild(@PathVariable String id) {
        RestResponse restResponse = null;
        try {
            Object object = structureFormService.getChild(id);
            restResponse = RestResponse.succeed(object);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 逻辑删除主表并且删除子表
     *
     * @param ids
     * @return
     */
    @PostMapping("/del")
    public RestResponse delete(@RequestBody String ids) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            structureFormService.delete(idList);
            jsonObject.put("message", "删除成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "删除失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 单独审核
     *
     * @param id
     * @param checkVo
     * @return
     */
    @PostMapping("/check/{id}")
    public RestResponse check(@PathVariable String id, @RequestBody CheckVo checkVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.check(id, checkVo);
            jsonObject.put("message", "审核成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "审核失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 批量审核
     *
     * @param checkVo
     * @return
     */
    @PostMapping("/check")
    public RestResponse checkMul(@RequestBody CheckVo checkVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.check(checkVo);
            jsonObject.put("message", "审核成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "审核失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 批量提交
     *
     * @param ids
     * @return
     */
    @PostMapping("/submit")
    public RestResponse submit(@RequestBody String ids) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            List<String> idList = Arrays.asList(ids.split(","));
            structureFormService.submit(idList);
            jsonObject.put("message", "提交成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "提交失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 获取任务编号
     *
     * @return
     */
    @GetMapping("structureCode")
    public RestResponse getStructureCode() {
        RestResponse restResponse = null;
        try {
            String s = DivisionCache.structureCode();
            restResponse = RestResponse.succeed(s);
        } catch (Exception e) {
            String errormessage = "获取失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 下载砖混结构模板
     *
     * @return
     */
    @GetMapping("downMould")
    public RestResponse downMould() {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String excel = "DeptFileTemplate.xlsx";
            downloadUtils.download(excel);
            jsonObject.put("message", "下载成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "下载失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 保存附件
     *
     * @param formId
     * @param type
     * @param fileId
     * @return
     */
    @PostMapping("uploadFile/{formId}/{type}/{fileId}")
    public RestResponse updateFile(@PathVariable("formId") String formId, @PathVariable("type") String type, @PathVariable("fileId") String fileId) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.updateFile(formId, type, fileId);
            jsonObject.put("message", "保存成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "保存失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }
    

    /**
     * 导出前选中类型，判断该类型查询到的数据是否为空
     *
     * @param structureQueryVo
     * @return
     */
    @GetMapping("/isEmpty")
    public RestResponse isEmpty(StructureQueryVo structureQueryVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            Boolean isEmpty = structureFormService.isEmpty(structureQueryVo);
            if (isEmpty) {
                jsonObject.put("message", "是");
            } else {
                jsonObject.put("message", "否");
            }
            restResponse = RestResponse.succeed(jsonObject);

        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导出
     *
     * @param structureQueryVo
     * @param response
     * @return
     */
    @GetMapping("/export")
    public RestResponse export(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.export(structureQueryVo, response);
            jsonObject.put("message", "导出成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 按照类型结构统计---栋数
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("/buildingstype")
    public RestResponse findBybuildingsCount(AnalysisQueryVo analysisQueryVo) {
        JSONObject jsonObject = new JSONObject();
        RestResponse restResponse = null;
        try {
            //栋数柱状图
            String status = analysisQueryVo.getStatus();
            Map<String, Object> buildCountMap = structureFormService.findbuildCountMap(analysisQueryVo);
            jsonObject.put("buildCountMap", buildCountMap);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            String errorMessage = "查询失败";
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }

   /*
    ** 按照类型结构统计---面积
     *
     * @param analysisQueryVo
     * @return
     */
    @GetMapping("/areacount")
    public RestResponse findByAreaCount(AnalysisQueryVo analysisQueryVo) {
        JSONObject jsonObject = new JSONObject();
        RestResponse restResponse = null;
        try {
            //面积柱状图
            Map<String, Object> areaCountMap = structureFormService.findAreaCountMap(analysisQueryVo);
            jsonObject.put("areaCountMap", areaCountMap);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            String errorMessage = "查询失败";
            restResponse = RestResponse.fail(errorMessage);
        }
        return restResponse;
    }


    /**
     * 导出房屋统计栋数
     *
     * @param structureQueryVo
     * @param response
     * @return
     */
    @GetMapping("/exportForBuildingCount")
    public RestResponse exportForBuildingCount(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.exportForBuildingCount(structureQueryVo, response);
            jsonObject.put("message", "导出成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 导出房屋统计面积
     *
     * @param structureQueryVo
     * @param response
     * @return
     */
    @GetMapping("/exportForBuildingArea")
    public RestResponse exportForBuildingArea(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.exportForBuildingArea(structureQueryVo, response);
            jsonObject.put("message", "导出成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = e.getMessage();
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 用于展示全国详查数据   详查模块
     *
     * @param request
     * @param structureQueryVo
     * @return
     */
    @GetMapping("checkForQgSift/page")
    public RestResponse getCheckForQgSiftPage(HttpServletRequest request, StructureQueryVo structureQueryVo) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = structureFormService.getCheckForQgSiftPage(structureQueryVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 多条件分页查询
     * @param request
     * @return
     */
    @GetMapping("getRole")
    public RestResponse getPage( HttpServletRequest request) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            String userId = PlatformSessionUtils.getUserId();
            List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
            List<String> roleName = new ArrayList<>();
            for (RoleEntity entity : roles) {
                roleName.add(entity.getRoleName());
            }
            jsonObject.put("roleName", roleName);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            e.printStackTrace();
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 单独退回
     *
     * @param id
     * @param checkVo
     * @return
     */
    @PostMapping("/back/{id}")
    public RestResponse back(@PathVariable String id, @RequestBody CheckVo checkVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.back(id, checkVo);
            jsonObject.put("message", "退回成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "退回失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 批量退回
     *
     * @param checkVo
     * @return
     */
    @PostMapping("/batchBack")
    public RestResponse batchBack(@RequestBody CheckVo checkVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.batchBack(checkVo);
            jsonObject.put("message", "退回成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "退回失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 专家数据管理模块   分页查询
     *
     * @param request
     * @param structureQueryVo
     * @return
     */
    @GetMapping("checkForExpertSift/page")
    public RestResponse checkForExpertSift(HttpServletRequest request, StructureQueryVo structureQueryVo) {
        RestResponse restResponse = null;
        try {
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            JSONObject jsonObject = structureFormService.checkForExpertSift(structureQueryVo, curPage, pageSize);
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }


    /**
     * 专家数据管理模块   导出前选中类型，判断该类型查询到的数据是否为空
     *
     * @param structureQueryVo
     * @return
     */
    @GetMapping("expertIsEmpty")
    public RestResponse expertIsEmpty(StructureQueryVo structureQueryVo) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            Boolean expertIsEmpty = structureFormService.expertIsEmpty(structureQueryVo);
            if (expertIsEmpty) {
                jsonObject.put("message", "是");
            } else {
                jsonObject.put("message", "否");
            }
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

    /**
     * 专家数据管理模块   导出
     *
     * @param structureQueryVo
     * @return
     */
    @GetMapping("expertExport")
    public RestResponse expertExport(StructureQueryVo structureQueryVo, HttpServletResponse response) {
        RestResponse restResponse = null;
        JSONObject jsonObject = new JSONObject();
        try {
            structureFormService.expertExport(structureQueryVo,response);
            jsonObject.put("message", "导出成功");
            restResponse = RestResponse.succeed(jsonObject);
        } catch (Exception e) {
            String errormessage = "查询失败";
            log.error(errormessage, e);
            restResponse = RestResponse.fail(errormessage);
        }
        return restResponse;
    }

}
